// 这里进行 new 和使用
import LRequest from './request' // 导入封装好的axios
import { BASE_URL, TIME_OUT } from './request/config' // 导入默认url
import localCache from '@/utils/cache'

// 使用 实例化封装好的 axios类
const lRequest = new LRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config) => {
      // 携带token拦截
      let token = localCache.getCache('token')
      if (token) {
        config.headers!.Authorization = `Bearer ${token}`
      }
      // console.log(`单个请求成功的拦截`)
      return config
    },
    requestInterceptorCatch: (err) => {
      // console.log(`请求失败的拦截`)
      return err
    },
    responseInterceptor: (res) => {
      // console.log(`单个响应成功的拦截`)
      return res
    },
    responseInterceptorCatch: (err) => {
      // console.log(`响应失败的拦截`)
      return err
    }
  }
})
export default lRequest
